import { editUrlRelateUsingPost} from '@/services/backend/shortLinkController';
import { Button, Form, Input, Select } from 'antd';
import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';
import { SUCCESS_CODE } from '@/constants';

interface Props {
  oldData?: any;
  visible: boolean;
  onSubmit: () => void;
  onCancel: () => void;
}

/**
 * 更新节点
 *
 * @param fields
 */


/**
 * 更新弹窗
 * @param props
 * @constructor
 */


const UpdateModal: React.FC<Props> = (props) => {
  let { oldData, visible, onSubmit, onCancel } = props;
  const formRef = React.useRef<any>(null);
  let newArray =[
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        0: { text: '草稿'},
        1: { text: '发布'},
        2: { text: '下架'},
      },
    },
  ]
  
  // 更新链接信息（管理员）
  const handleUpdate = async (values:any) => {
   const fields = {
      id: oldData.id,
      title: values.title,
      sortUrl: values.sortUrl,
      status: values.status,
    }
    const res = await editUrlRelateUsingPost(fields);
    if(res.code === SUCCESS_CODE){
      oldData = res.data;
      message.success(res.message);
      onSubmit?.();
    }
  };

  if (!oldData) {
    return <></>;
  }
  return (
    <Modal
      destroyOnClose
      title={'更新'}
      open={visible}
      footer={null}
      onCancel={() => {
        onCancel?.();
      }}
    >
      <Form
        autoComplete="off"
        onFinish={handleUpdate}
        initialValues={oldData}
      >
        <Form.Item
          label="标题"
          name="title"
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="短链"
          name="sortUrl"
        >
          <Input disabled/>
        </Form.Item>
        <Form.Item
          label="状态"
          name="status"
        >
          <Select 
          options={[
            { value: 0, label: '草稿' },
            { value: 1, label: '发布' },
            { value: 2, label: '下架' },
          ]}
          />
        </Form.Item>
        <Form.Item>
        <Button type="primary" htmlType="submit">
          提交
        </Button>
        </Form.Item>
      </Form>
      {/* <ProTable
        type="form"
        columns={newArray}
        form={{
          initialValues: oldData,
        }}
        onSubmit={async (values: API.UserUpdate) => {
          const res = await handleUpdate({
            ...values,
            id: oldData.id as any,
          });
          if (res) {
            onSubmit?.();
          }
        }}
      /> */}
    </Modal>
  );
};
export default UpdateModal;
